<?php
/**
 * category_tree Class.
 *
 * @package classes
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: category_tree.php 3041 2006-02-15 21:56:45Z wilt $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
/**
 * category_tree Class.
 * This class is used to generate the category tree used for the categories sidebox
 *
 * @package classes
 */

class category_tree extends base {

var $category_string='';
  function zen_category_tree($product_type = "all") {
    global $db, $cPath, $cPath_array,$this_is_home_page;
    if ($product_type != 'all') {
      $sql = "select type_master_type from " . TABLE_PRODUCT_TYPES . "
                where type_master_type = " . $product_type . "";
      $master_type_result = $db->Execute($sql);
      $master_type = $master_type_result->fields['type_master_type'];
    }
    if ($product_type == 'all') {
      $categories_query = "select c.categories_id, cd.categories_name, c.parent_id, c.categories_image
                             from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                             where c.parent_id = 0
                             and c.categories_id = cd.categories_id
                             and cd.language_id='" . (int)$_SESSION['languages_id'] . "'
                             and c.categories_status= 1
                             order by sort_order, cd.categories_name";
    } else {
      $categories_query = "select ptc.category_id as categories_id, cd.categories_name, c.parent_id, c.categories_image
                             from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd, " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc
                             where c.parent_id = 0
                             and ptc.category_id = cd.categories_id
                             and ptc.product_type_id = " . $master_type . "
                             and c.categories_id = ptc.category_id
                             and cd.language_id=" . (int)$_SESSION['languages_id'] ."
                             and c.categories_status= 1
                             order by sort_order, cd.categories_name";
    }
    $categories = $db->Execute($categories_query, '', true, 150);
     if($this_is_home_page){
     	$this->category_string.='<li id="Home" class="home"><div class="nav-text"><a title="Home" href="' . HTTP_SERVER . DIR_WS_CATALOG . '" class="select">Home</a></div></li>';
     }else{
     	$this->category_string.='<li id="Home" class="home"><div class="nav-text"><a title="Home" href="' . HTTP_SERVER . DIR_WS_CATALOG . '">Home</a></div></li>';
     }
       
    while (!$categories->EOF)  {
    	$this->category_string.='<li class="'. str_replace(' ','',$categories->fields['categories_name']).'">';
      //查找有二级栏目的顶级栏目
        $top_hasson_categories_query = "select c.categories_id, cd.categories_name, c.parent_id, c.categories_image
                             from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
                             where c.parent_id =" .  $categories->fields['categories_id'] . "
                             and c.categories_id = cd.categories_id
                             and cd.language_id='" . (int)$_SESSION['languages_id'] . "'
                             and c.categories_status= 1
                             order by sort_order, cd.categories_name";
         $top_hasson_categories = $db->Execute($top_hasson_categories_query, '', true, 150);
         if($top_hasson_categories->RecordCount()>0){
         if (isset($cPath_array) && in_array( $categories->fields['categories_id'], $cPath_array)){
         	$this->category_string.='<div class="nav-text nav-hasSon"><a class="current"  href="' .zen_href_link(FILENAME_DEFAULT,  'cPath=' .$categories->fields['categories_id']) . '">'.$categories->fields['categories_name'].'</a></div>';
         }else{
         	$this->category_string.='<div class="nav-text nav-hasSon"><a  href="' .zen_href_link(FILENAME_DEFAULT,  'cPath=' .$categories->fields['categories_id']) . '">'.$categories->fields['categories_name'].'</a></div>';
         	
         }
         	$this->category_string.='<ul>';
         	while (!$top_hasson_categories->EOF) {
         	 if (isset($cPath_array) && in_array( $top_hasson_categories->fields['categories_id'], $cPath_array)){
         	    $this->category_string.='<li><a class="current" href="' . zen_href_link(FILENAME_DEFAULT,  'cPath=' .$categories->fields['categories_id'].'_'.$top_hasson_categories->fields['categories_id']) . '">'.$top_hasson_categories->fields['categories_name'].'</a></li>';
         	 }else{
         	 	$this->category_string.='<li><a  href="' . zen_href_link(FILENAME_DEFAULT,  'cPath=' .$categories->fields['categories_id'].'_'.$top_hasson_categories->fields['categories_id']) . '">'.$top_hasson_categories->fields['categories_name'].'</a></li>';
         	 }
         	  $top_hasson_categories->MoveNext();
         	}
         	 
         	$this->category_string.='</ul>' ; 
         	$this->category_string.='';
         }else{
         	 if (isset($cPath_array) && in_array( $categories->fields['categories_id'], $cPath_array)){
         	    $this->category_string.='<div class="nav-text"><a class="current" href="' . zen_href_link(FILENAME_DEFAULT,  'cPath=' .$categories->fields['categories_id']) . '">'.$categories->fields['categories_name'].'</a></div>';
         	 }else{
         	 	$this->category_string.='<div class="nav-text"><a  href="' . zen_href_link(FILENAME_DEFAULT,  'cPath=' .$categories->fields['categories_id']) . '">'.$categories->fields['categories_name'].'</a></div>';
         	 }
         }
       $this->category_string.='</li>';
          $categories->MoveNext();
    }
    

 
}
}
?>